﻿using System;
using System.Data;
using BLL;
using DAL;
using Ext.Net;
using Roles;

namespace ERP.Modules
{
    public partial class Expenditure : System.Web.UI.UserControl
    {
        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
            {
                CheckPermisstion();
                GetExpenditure();
                btnEdit.Disable(true);
                btnDel.Disable(true);
                btnExport.Disable(true);
                btnGetSellCode.Disable(true);
            }
        }
        //Store
        protected void StoreExpenditure_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            txtSearch.Text = string.Empty;
            txtDateBeginFillter.Text = string.Empty;
            txtDateEndFillter.Text = string.Empty;
            GetExpenditure();
        }
        protected void StoreCustomerExpenditure_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            GetCustomer();
        }
        protected void StoreSellExpenditure_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            GetOrderBuy();
        }
        //Button
        protected void btnAddNew_Click(object sender, DirectEventArgs e)
        {
            GetAutoID();
            btnReloadCode.Enable(true);
            GetReceiptReason();
            ClearTextBox();
            WindowsExpenditure.Show();
        }
        protected void btnEdit_Click(object sender, DirectEventArgs e)
        {
            var sm = grExpenditure.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    try
                    {
                        GetReceiptReason();
                        btnReloadCode.Disable(true);
                        btnGetSellCode.Disable(true);
                        if (String.IsNullOrEmpty(txtIDSell.Text))
                        {
                            GetExpenditureEdit(int.Parse(row.RecordID));
                            WindowsExpenditure.Show();
                        }
                        else
                        {
                            GetExpenditureEdit(int.Parse(row.RecordID));
                            GetExpenditureTotal(int.Parse(txtIDSell.Text), float.Parse(txtVoucherAmountExpenditure.Text));
                            WindowsExpenditure.Show();
                        }
                    }
                    catch (Exception)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                    }
                }
        }
        protected void btnDel_Click(object sender, DirectEventArgs e)
        {
            var sm = grExpenditure.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                foreach (var row in sm.SelectedRows)
                {
                    try
                    {
                        var obj = new EtExpenditure { ID = int.Parse(row.RecordID) };
                        BllExpenditure.DeleteExpenditure(obj);
                        if (obj.Msg == 0)
                            HTML.clsFormat.Alert_ERROR("Thông Báo", "Tồn tại chứng từ liên quan, không xoá được vui lòng xoá chứng từ trước !");
                    }
                    catch (Exception)
                    {
                        HTML.clsFormat.Alert_ERROR("Thông Báo", "Tồn tại tài liệu con, không xoá được vui lòng xoá tài liệu con trước !");
                    }
                }
            GetExpenditure();
        }
        protected void btnSavePopUp_Click(object sender, DirectEventArgs e)
        {
            Insert();
        }
        protected void btnCancelPopUp_Click(object sender, DirectEventArgs e)
        {
            WindowsExpenditure.Close();
        }
        protected void btnReloadCode_Click(object sender, DirectEventArgs e)
        {
            GetAutoID();
        }
        protected void btnGetPayerRecipientCode_Click(object sender, DirectEventArgs e)
        {
            var sm = grCustomerExpenditure.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null) sm.DeselectAll();
            GetCustomer();
            WindowCustomerExpenditure.Show();
        }
        protected void btnGetSellCode_Click(object sender, DirectEventArgs e)
        {
            var sm = grSellExpenditure.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null) sm.DeselectAll();
            GetOrderBuy();
            WindowSellExpenditure.Show();
        }
        protected void btnFillter_Click(object sender, DirectEventArgs e)
        {
            GetExpenditureFillterDate();
        }
        //Double-Click
        protected void ExpenditureDblClick(object sender, DirectEventArgs e)
        {
            GetReceiptReason();
            btnReloadCode.Disable(true);
            btnGetSellCode.Disable(true);
            int ID_Expenditure = int.Parse(e.ExtraParams["ID"]);
            if (String.IsNullOrEmpty(txtIDSell.Text))
            {
                GetExpenditureEdit(ID_Expenditure);
                WindowsExpenditure.Show();
            }
            else
            {
                GetExpenditureEdit(ID_Expenditure);
                if (float.Parse(txtVoucherAmountExpenditure.Text) > 0)
                {
                    GetExpenditureTotal(int.Parse(txtIDSell.Text), float.Parse(txtVoucherAmountExpenditure.Text));
                }
                WindowsExpenditure.Show();
            }
            
        }
        //Cell-Click
        protected void CellExpenditureCustomer_Click(object sender, DirectEventArgs e)
        {
            var sm = grCustomerExpenditure.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                string id = sm.SelectedCell.RecordID;
                GetCustomerEdit(int.Parse(id));
            }
            if (sm != null)
            {
                sm.Clear();
                sm.UpdateSelection();
            }
            WindowCustomerExpenditure.Close();
        }
        protected void CellSellExpenditure_Click(object sender, DirectEventArgs e)
        {
            var sm = grSellExpenditure.GetSelectionModel() as CellSelectionModel;
            if (sm != null)
            {
                string id = sm.SelectedCell.RecordID;
                GetOrderBuyEdit(int.Parse(id));
                GetOrderBuyTotal(int.Parse(id));
                sm.Clear();
                sm.UpdateSelection();
            }
        }
        //Search
        protected void Search_Event(object sender, DirectEventArgs e)
        {
            var _clsExpenditure = new BllExpenditure();
            var dtSearch = _clsExpenditure.GetExpenditure(txtSearch.Text);
            if (dtSearch != null && dtSearch.Rows.Count > 0)
            {
                StoreExpenditure.DataSource = dtSearch;
                StoreExpenditure.DataBind();
            }
            else
            {
                grExpenditure.GetStore().RemoveAll();
            }
        }
        //Select-Box
        protected void ReloadExpenditureReason(object sender, StoreReadDataEventArgs e)
        {
            GetReceiptReason();
        }
        protected void SelectBoxExpenditureReason_Change(object sender, DirectEventArgs e)
        {
            int IdReceiptReason = int.Parse(SelectBoxExpenditureReason.SelectedItem.Value);
            var obj = new EtReceiptReason { ID = IdReceiptReason };
            BllReceiptReason.GetReciptReasonCheck(obj);
            if (obj.Msg == 0)
            {
                btnGetSellCode.Disable(true);
                txtExpenditureReason_Note.AllowBlank = false;
                txtExpenditureReason_Note.BlankText = "Vui lòng điền vào lý do !";
                txtIDSell.Text = string.Empty;
                txtSellCode.Text = string.Empty;
            }
            else
            {
                btnGetSellCode.Enable(true);
                txtExpenditureReason_Note.AllowBlank = true;
                txtExpenditureReason_Note.BlankText = string.Empty;
            }
        }
        #endregion

        #region[Method]
        private void GetExpenditure()
        {
            var _clsExpenditure = new BllExpenditure();
            var _dtExpenditure = _clsExpenditure.GetExpenditure(txtSearch.Text);
            if (_dtExpenditure != null && _dtExpenditure.Rows.Count > 0)
            {
                StoreExpenditure.DataSource = _dtExpenditure;
                StoreExpenditure.DataBind();
            }
            else
            {
                grExpenditure.GetStore().RemoveAll();
            }
        }
        private void GetExpenditureTotal(int Order_Buy_Id,float VocherAmount)
        {
            var _clsExpenditureTotal = new BllExpenditure();
            var _dtExpenditureTotal = _clsExpenditureTotal.GetExpenditureTotal(Order_Buy_Id, VocherAmount);
            if (_dtExpenditureTotal != null && _dtExpenditureTotal.Rows.Count > 0)
            {
                txtAmountRemainExpenditure.Text = _dtExpenditureTotal.Rows[0]["Cast"].ToString();
            }
        }
        private void GetExpenditureEdit(int id)
        {
            var _clsExpenditureEdit = new BllExpenditure();
            var _dtExpenditureEdit = _clsExpenditureEdit.GetExpenditureEdit(id);
            if (_dtExpenditureEdit != null && _dtExpenditureEdit.Rows.Count > 0)
            {
                txtExpenditure_ID.Text = _dtExpenditureEdit.Rows[0]["ID"].ToString();
                txtIDCustomer.Text = _dtExpenditureEdit.Rows[0]["Customer_ID"].ToString();
                txtIDSell.Text = _dtExpenditureEdit.Rows[0]["Order_Buy_Id"].ToString();
                txtExpenditure_Alias.Text = _dtExpenditureEdit.Rows[0]["ExpenditureCode"].ToString();
                txtExpenditureDate.Text = _dtExpenditureEdit.Rows[0]["ExpenditureDate"].ToString();
                SelectBoxExpenditureReason.SetValue(int.Parse(_dtExpenditureEdit.Rows[0]["IDReceiptReason"].ToString()));
                txtPayerRecipientCode.Text = _dtExpenditureEdit.Rows[0]["Customer_Name"].ToString();
                txtSellCode.Text = _dtExpenditureEdit.Rows[0]["Order_Buy_Code"].ToString();
                txtAddressCustomer.Text = _dtExpenditureEdit.Rows[0]["Customer_Address"].ToString();
                txtVoucherAmountExpenditure.Text = _dtExpenditureEdit.Rows[0]["VoucherAmount"].ToString();
                txtAmountExpenditure.Text = _dtExpenditureEdit.Rows[0]["Amount"].ToString();
                txtExpenditureReason_Note.Text = _dtExpenditureEdit.Rows[0]["Note"].ToString();
                if (float.Parse(txtVoucherAmountExpenditure.Text) > 0)
                {
                    txtAmountRemainExpenditure.Text = Convert.ToString(float.Parse(txtVoucherAmountExpenditure.Text) -
                                                                      float.Parse(txtAmountExpenditure.Text));
                }
                else
                {
                    txtAmountRemainExpenditure.Text = "0";
                }
            }
        }
        private void GetExpenditureFillterDate()
        {
            var _clsExpenditure = new BllExpenditure();
            if (String.IsNullOrEmpty(txtDateBeginFillter.Text) && String.IsNullOrEmpty(txtDateEndFillter.Text))
            {
                var _dtGetExpenditure = _clsExpenditure.GetExpenditure(txtSearch.Text);
                if (_dtGetExpenditure != null && _dtGetExpenditure.Rows.Count > 0)
                {
                    StoreExpenditure.DataSource = _dtGetExpenditure;
                    StoreExpenditure.DataBind();
                }
                else
                {
                    grExpenditure.GetStore().RemoveAll();
                }
            }
            else
            {
                var _dtGetExpenditure = _clsExpenditure.GetExpenditureFillterDate(txtDateBeginFillter.Text, txtDateEndFillter.Text);
                if (_dtGetExpenditure != null && _dtGetExpenditure.Rows.Count > 0)
                {
                    StoreExpenditure.DataSource = _dtGetExpenditure;
                    StoreExpenditure.DataBind();
                }
                else
                {
                    grExpenditure.GetStore().RemoveAll();
                }
            }
        }
        private void GetReceiptReason()
        {
            var _clsReceiptReason = new BllExpenditureReason();
            var _dtReceiptReason = _clsReceiptReason.GetExpenditureReason(string.Empty);
            if (_dtReceiptReason != null && _dtReceiptReason.Rows.Count > 0)
            {
                StoreExpenditureReason.DataSource = _dtReceiptReason;
                StoreExpenditureReason.DataBind();
            }
            else
            {
                SelectBoxExpenditureReason.GetStore().RemoveAll();
            }
        }
        private void GetCustomer()
        {
            var _clsCustomer = new Customer_BLL();
            var _dtCustomer = _clsCustomer.BindGirdtb_Customer(string.Empty);
            if (_dtCustomer != null && _dtCustomer.Rows.Count > 0)
            {
                StoreCustomerExpenditure.DataSource = _dtCustomer;
                StoreCustomerExpenditure.DataBind();
            }
            else
            {
                grCustomerExpenditure.GetStore().RemoveAll();
            }
        }
        private void GetCustomerEdit(int id)
        {
            var _clsCustomer = new Customer_BLL();
            var _dtCustomerEdit = _clsCustomer.GetCustomerDetail(id);
            if (_dtCustomerEdit != null && _dtCustomerEdit.Rows.Count > 0)
            {
                txtIDCustomer.Text = _dtCustomerEdit.Rows[0]["Customer_ID"].ToString();
                txtPayerRecipientCode.Text = _dtCustomerEdit.Rows[0]["Customer_Name"].ToString();
                txtAddressCustomer.Text = _dtCustomerEdit.Rows[0]["Customer_Address"].ToString();
            }
        }
        private void GetOrderBuy()
        {
            var _clsOrderBuy = new Order_Buy_BLL();
            var _dtGetOrderBuy = _clsOrderBuy.GetOrderBuy(string.Empty);
            if (_dtGetOrderBuy != null && _dtGetOrderBuy.Rows.Count > 0)
            {
                StoreSellExpenditure.DataSource = _dtGetOrderBuy;
                StoreSellExpenditure.DataBind();
            }
            else
            {
                grSellExpenditure.GetStore().RemoveAll();
            }
        }
        private void GetOrderBuyEdit(int id)
        {
            var _clsOrderBuyEdit = new Order_Buy_BLL();
            var _dtGetOrderBuyEdit = _clsOrderBuyEdit.GetOrderBuyEdit(id);
            if (_dtGetOrderBuyEdit != null && _dtGetOrderBuyEdit.Rows.Count > 0)
            {
                var obj = new EtExpenditure { Order_Buy_Id = id, Flag = 0 };
                BllExpenditure.GetExpenditureCheck(obj);
                if (obj.Msg == 1)
                {
                    HTML.clsFormat.Alert_ERROR("Lỗi", "Chứng từ này đã được thanh toán !");
                }
                else
                {
                    txtIDSell.Text = _dtGetOrderBuyEdit.Rows[0]["Order_Buy_Id"].ToString();
                    txtSellCode.Text = _dtGetOrderBuyEdit.Rows[0]["Order_Buy_Code"].ToString();
                    WindowSellExpenditure.Close();
                }
            }
        }
        private void GetOrderBuyTotal(int id)
        {
            var _clsTotal = new BllExpenditure();
            var _dtTotal = _clsTotal.GetExpenditureTotal(id, 0);
            if (_dtTotal != null && _dtTotal.Rows.Count > 0)
            {
                var obj = new EtExpenditure { Order_Buy_Id = id, Flag = 0 };
                BllExpenditure.GetExpenditureCheck(obj);
                if (obj.Msg == 1)
                {
                    HTML.clsFormat.Alert_ERROR("Lỗi", "Chứng từ này đã được thanh toán !");
                }
                else
                {
                    txtVoucherAmountExpenditure.Text = String.Format("{0:0,00}", _dtTotal.Rows[0]["Total"]).Replace(".", ",");
                    GetExpenditureTotal(id, float.Parse(_dtTotal.Rows[0]["Total"].ToString()));
                    WindowSellExpenditure.Close();
                }
            }
        }
        private void GetAutoID()
        {
            var _clsAutoID = new BllEmployee();
            var _dtGetAutoID = _clsAutoID.GetAuto_ID("ExpenditureCode", "tb_Expenditure", "1");
            if (_dtGetAutoID != null && _dtGetAutoID.Rows.Count > 0)
            {
                try
                {
                    txtExpenditure_Alias.Text = "PC" + DateTime.Now.ToString("yyMM") + "-0" +
                                            Convert.ToString(int.Parse(_dtGetAutoID.Rows[0]["Auto_ID"].ToString()) +
                                                             int.Parse(1.ToString()));
                }
                catch (Exception ex)
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", ex.Message);
                }
            }
        }
        private void Insert()
        {
            if (String.IsNullOrEmpty(txtExpenditure_ID.Text))
            {
                InsertValue();
            }
            else
            {
                if (CheckPermisstionDobclick())
                {
                    HTML.clsFormat.Alert_ERROR("Thông Báo", "Bạn không được cấp quyền để sửa đổi !");
                }
                else
                {
                    UpdateValue();
                }
            }
        }
        private bool CheckInsertExpenditure()
        {
            bool kiemtra = false;
            var _clsExpenditureCheckInsert = new BllExpenditure();
            var _dtExpenditureCheckInsert = _clsExpenditureCheckInsert.GetExpenditure(string.Empty);
            if (_dtExpenditureCheckInsert != null && _dtExpenditureCheckInsert.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtExpenditureCheckInsert.Rows)
                {
                    if (txtExpenditure_Alias.Text == dr["ExpenditureCode"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        private void InsertValue()
        {
            try
            {
                if (CheckInsertExpenditure())
                {
                    HTML.clsFormat.Alert_ERROR("Lỗi", "Mã phiếu chi " + txtExpenditure_Alias.Text + " đã tồn tại !");
                }
                else
                {
                    if (txtIDSell.Text != string.Empty)
                    {
                        #region[Check]
                        var _objCheck = new EtExpenditure
                        {
                            Order_Buy_Id = int.Parse(txtIDSell.Text),Amount = float.Parse(txtAmountExpenditure.Text),
                            VoucherAmount = float.Parse(txtVoucherAmountExpenditure.Text)
                        };
                        BllExpenditure.GetExpenditureCheckAmout(_objCheck);
                        if (float.Parse(txtAmountExpenditure.Text) > float.Parse(txtVoucherAmountExpenditure.Text))
                        {
                            HTML.clsFormat.Alert_ERROR("Lỗi",
                                "Số tiền thanh toán không được vượt quá số tiền theo chứng từ !");
                        }
                        else if (_objCheck.Msg == 0)
                        {
                            HTML.clsFormat.Alert_ERROR("Lỗi",
                                "Tổng số tiền thanh toán không được vượt quá số tiền theo chứng từ !");
                        }
                        else
                        {
                            var obj = new EtExpenditure
                            {
                                ExpenditureCode = txtExpenditure_Alias.Text,
                                ExpenditureDate = DateTime.Parse(txtExpenditureDate.Text),
                                IDReceiptReason = int.Parse(SelectBoxExpenditureReason.SelectedItem.Value),
                                PayerRecipientCode = int.Parse(txtIDCustomer.Text),
                                Order_Buy_Id = String.IsNullOrEmpty(txtIDSell.Text)
                                        ? (int?)null
                                        : int.Parse(txtIDSell.Text),
                                VoucherFile = null,
                                VoucherAmount =
                                    String.IsNullOrEmpty(txtVoucherAmountExpenditure.Text)
                                        ? (float?)null
                                        : float.Parse(txtVoucherAmountExpenditure.Text.Replace(",", "").Replace(".", "")),
                                Amount =
                                    String.IsNullOrEmpty(txtAmountExpenditure.Text)
                                        ? (float?)null
                                        : float.Parse(txtAmountExpenditure.Text.Replace(",", "").Replace(".", "")),
                                Note = txtExpenditureReason_Note.Text,
                                Amount_Remain = String.IsNullOrEmpty(txtAmountRemainExpenditure.Text)
                                        ? float.Parse(txtVoucherAmountExpenditure.Text.Replace(",", "").Replace(".", "")) - float.Parse(txtAmountExpenditure.Text.Replace(",", "").Replace(".", ""))
                                        : float.Parse(txtAmountRemainExpenditure.Text.Replace(",", "").Replace(".", "")) - float.Parse(txtAmountExpenditure.Text.Replace(",", "").Replace(".", ""))
                            };
                            BllExpenditure.InsertExpenditure(obj);
                            HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công phiếu chi !");
                            ClearTextBox();
                            GetExpenditure();
                            GetAutoID();
                        }
                        #endregion
                    }
                    else
                    {
                        #region[Uncheck]

                        var obj = new EtExpenditure
                        {
                            ExpenditureCode = txtExpenditure_Alias.Text,
                            ExpenditureDate = DateTime.Parse(txtExpenditureDate.Text),
                            IDReceiptReason = int.Parse(SelectBoxExpenditureReason.SelectedItem.Value),
                            PayerRecipientCode = int.Parse(txtIDCustomer.Text),
                            Order_Buy_Id = String.IsNullOrEmpty(txtIDSell.Text)
                                ? (int?) null
                                : int.Parse(txtIDSell.Text),
                            VoucherFile = null,
                            VoucherAmount = 0,
                            Amount = float.Parse(txtAmountExpenditure.Text.Replace(",", "").Replace(".", "")),
                            Note = txtExpenditureReason_Note.Text
                        };
                        BllExpenditure.InsertExpenditure(obj);
                        HTML.clsFormat.Alert_INFO("Thông Báo", "Thêm mới thành công phiếu chi !");
                        ClearTextBox();
                        GetExpenditure();
                        GetAutoID();
                        #endregion
                    }
                }
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        private void UpdateValue()
        {
            try
            {
                if (txtIDSell.Text != string.Empty)
                {
                    #region[Check]
                    var _objCheck = new EtExpenditure
                    {
                        Order_Buy_Id = int.Parse(txtIDSell.Text),Amount = float.Parse(txtAmountExpenditure.Text),
                        VoucherAmount = float.Parse(txtVoucherAmountExpenditure.Text)
                    };
                    BllExpenditure.GetExpenditureCheckAmout(_objCheck);
                    if (float.Parse(txtAmountExpenditure.Text) > float.Parse(txtVoucherAmountExpenditure.Text))
                    {
                        HTML.clsFormat.Alert_ERROR("Lỗi",
                            "Số tiền thanh toán không được vượt quá số tiền theo chứng từ !");
                    }
                    else if (_objCheck.Msg == 0)
                    {
                        HTML.clsFormat.Alert_ERROR("Lỗi",
                            "Tổng số tiền thanh toán không được vượt quá số tiền theo chứng từ !");
                    }
                    if (float.Parse(txtAmountExpenditure.Text) > float.Parse(txtVoucherAmountExpenditure.Text))
                    {
                        HTML.clsFormat.Alert_ERROR("Lỗi",
                            "Số tiền thanh toán không được vượt quá số tiền theo chứng từ !");
                    }
                    else
                    {
                        var obj = new EtExpenditure
                        {
                            ID = int.Parse(txtExpenditure_ID.Text),
                            ExpenditureCode = txtExpenditure_Alias.Text,
                            ExpenditureDate = DateTime.Parse(txtExpenditureDate.Text),
                            IDReceiptReason = int.Parse(SelectBoxExpenditureReason.SelectedItem.Value),
                            PayerRecipientCode = int.Parse(txtIDCustomer.Text),
                            Order_Buy_Id = String.IsNullOrEmpty(txtIDSell.Text)
                                        ? (int?)null
                                        : int.Parse(txtIDSell.Text),
                            VoucherFile = null,
                            VoucherAmount =
                                String.IsNullOrEmpty(txtVoucherAmountExpenditure.Text)
                                    ? (float?)null
                                    : float.Parse(txtVoucherAmountExpenditure.Text.Replace(",", "").Replace(".", "")),
                            Amount =
                                String.IsNullOrEmpty(txtAmountExpenditure.Text)
                                    ? (float?)null
                                    : float.Parse(txtAmountExpenditure.Text.Replace(",", "").Replace(".", "")),
                            Note = txtExpenditureReason_Note.Text,
                            Amount_Remain = String.IsNullOrEmpty(txtAmountRemainExpenditure.Text)
                                        ? float.Parse(txtVoucherAmountExpenditure.Text.Replace(",", "").Replace(".", "")) - float.Parse(txtAmountExpenditure.Text.Replace(",", "").Replace(".", ""))
                                        : float.Parse(txtAmountRemainExpenditure.Text.Replace(",", "").Replace(".", "")) - float.Parse(txtAmountExpenditure.Text.Replace(",", "").Replace(".", ""))
                        };
                        BllExpenditure.UpdateExpenditure(obj);
                        if (obj.Msg == 0)
                        {
                            HTML.clsFormat.Alert_ERROR("Lỗi", "Chứng từ này đã bị khoá !");
                        }
                        else
                        {
                            HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật thành công phiếu chi !");
                            WindowsExpenditure.Close();
                            GetExpenditure();
                        }
                    }
                    #endregion
                }
                else
                {
                    #region[Uncheck]

                    var obj = new EtExpenditure
                    {
                        ID = int.Parse(txtExpenditure_ID.Text),
                        ExpenditureCode = txtExpenditure_Alias.Text,
                        ExpenditureDate = DateTime.Parse(txtExpenditureDate.Text),
                        IDReceiptReason = int.Parse(SelectBoxExpenditureReason.SelectedItem.Value),
                        PayerRecipientCode = int.Parse(txtIDCustomer.Text),
                        Order_Buy_Id = String.IsNullOrEmpty(txtIDSell.Text)
                            ? (int?) null
                            : int.Parse(txtIDSell.Text),
                        VoucherFile = null,
                        VoucherAmount = 0,
                        Amount = float.Parse(txtAmountExpenditure.Text.Replace(",", "").Replace(".", "")),
                        Note = txtExpenditureReason_Note.Text
                    };
                    BllExpenditure.UpdateExpenditure(obj);
                    if (obj.Msg == 0)
                    {
                        HTML.clsFormat.Alert_ERROR("Lỗi", "Chứng từ này đã bị khoá !");
                    }
                    else
                    {
                        HTML.clsFormat.Alert_INFO("Thông Báo", "Cập nhật thành công phiếu chi !");
                        WindowsExpenditure.Close();
                        GetExpenditure();
                    }
                    #endregion
                }
                
            }
            catch (Exception ex)
            {
                HTML.clsFormat.Alert_ERROR("Lỗi", ex.Message);
            }
        }
        #endregion

        #region[Print]
        protected void MenuItemExportExcel_Click(object sender, DirectEventArgs e)
        {
            var _clsGetReport = new BllReportSell();
            var _clsGetServerUpload = new BLLsaddicServerUpload();
            var _dtGetServerUpload = _clsGetServerUpload.GetServerUpload();
            var sm = grExpenditure.GetSelectionModel() as CheckboxSelectionModel;
            if (sm != null)
                if (sm.SelectedRows.Count > 1)
                {
                    HTML.clsFormat.Alert_ERROR("Lỗi", "Bạn chỉ được chọn một dòng để in dữ liệu !");
                }
                else
                {
                    foreach (var row in sm.SelectedRows)
                    {
                        var _dtGetReport = _clsGetReport.GetReportExpenditure(int.Parse(row.RecordID));
                        if (_dtGetReport != null && _dtGetReport.Rows.Count > 0)
                        {
                            if (_dtGetServerUpload != null && _dtGetServerUpload.Rows.Count > 0)
                            {
                                LibGlobal.Export.ExcelExport(_dtGetReport,
                                    Server.MapPath(_dtGetServerUpload.Rows[4]["Path_Download"].ToString()),
                                    Server.MapPath(_dtGetServerUpload.Rows[4]["Path_Upload"].ToString()));
                                LibGlobal.Export.DownloadFile(
                                    _dtGetServerUpload.Rows[4]["Path_Download"].ToString(),
                                    _dtGetServerUpload.Rows[4]["Page_Name"].ToString(),
                                    _dtGetServerUpload.Rows[4]["Extend"].ToString());
                            }
                        }
                    }
                    HTML.clsFormat.Alert_INFO("Thông báo", "In thành công !");
                }
        }
        #endregion

        #region[Clear-Text]
        private void ClearTextBox()
        {
            txtExpenditure_ID.Text = string.Empty;
            txtIDCustomer.Text = string.Empty;
            txtIDSell.Text = string.Empty;
            txtExpenditureDate.Text = DateTime.Now.ToString();
            SelectBoxExpenditureReason.Text = string.Empty;
            txtPayerRecipientCode.Text = string.Empty;
            txtSellCode.Text = string.Empty;
            txtAddressCustomer.Text = string.Empty;
            txtVoucherAmountExpenditure.Text = string.Empty;
            txtAmountExpenditure.Text = string.Empty;
            txtAmountRemainExpenditure.Text = string.Empty;
            txtExpenditureReason_Note.Text = string.Empty;
            btnGetSellCode.Disable(true);
        }
        #endregion

        #region[Permisstion]
        private void CheckPermisstion()
        {
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Expenditure, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnAdd == false)
                {
                    btnAddNew.Visible = false;
                }
                if (Is_BtnEdit == false)
                {
                    btnEdit.Visible = false;
                }
                if (Is_BtnView == false)
                {
                    PanelExpenditure.Visible = false;
                }
                if (Is_BtnDel == false)
                {
                    btnDel.Visible = false;
                }
                if (Is_BtnPrint == false)
                {
                    btnExport.Visible = false;
                }
            }
        }
        private bool CheckPermisstionDobclick()
        {
            var check = false;
            if (Session["login"] != null && Session["USer_Detail_Id"].ToString() != string.Empty)
            {
                bool Is_BtnAdd;
                bool Is_BtnEdit;
                bool Is_BtnView;
                bool Is_BtnDel;
                bool Is_BtnPrint;
                bool Is_BtnCancel;
                bool Is_BtnReview;

                SecurityServices.HasPermisstion_Btn(StringContent.Expenditure, out Is_BtnAdd, out Is_BtnEdit, out Is_BtnDel, out Is_BtnView, out Is_BtnCancel, out Is_BtnPrint, out Is_BtnReview);
                if (Is_BtnEdit == false)
                {
                    check = true;
                }
            }
            return check;
        }
        #endregion
    }
}